package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.pojo.DtsPermission;
import com.qf.service.PermissionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PermissionServiceimpl implements PermissionService {

    @Autowired
    DtsPermissionMapper dtsPermissionMapper;
    @Override
    public Set<String> queryByIds(Integer[] roleIds) {

        Set<String> permissions = new HashSet<>();

        QueryWrapper<DtsPermission> wrapper = new QueryWrapper<>();

        wrapper.in("role_id", Arrays.asList(roleIds));
        wrapper.eq("deleted",false);

        List<DtsPermission> list = dtsPermissionMapper.selectList(wrapper);
        for(DtsPermission dtsPermission : list){
            permissions.add(dtsPermission.getPermission());
        }
        System.out.println(list);
        return permissions;
    }

    @Override
    public void insert(int roleId, List<String> permisssions) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id",roleId);
        wrapper.eq("deleted",false);

        dtsPermissionMapper.delete(wrapper);

        for(int i=0;i<permisssions.size();i++){
            DtsPermission permission = new DtsPermission();
            permission.setAddTime(new Date());
            permission.setDeleted(false);
            permission.setPermission(permisssions.get(i));
            permission.setRoleId(roleId);
            permission.setUpdateTime(new Date());
            dtsPermissionMapper.insert(permission);
        }
    }
}
